package architecture;

//ControlUnit
public class ControlUnit{
	private boolean funcCodeAvailable;
	MyInt opCode;
	MyInt funcCode;
	
	MyInt regDst;
	MyInt regWrite;
	MyInt ALUSrc;
	MyInt ALUOp;
	MyInt memWrite;
	MyInt memRead;
	MyInt memToReg;
	//MyInt pcSrc;
	void clockEvent(){
		funcCodeAvailable=false;
		switch(opCode.i){
		case 0x23:
			regDst.i  =0;
			regWrite.i=1;
			ALUSrc.i  =1;
			ALUOp.i   =0x20;
			memWrite.i=0;
			memRead.i =1;
			memToReg.i=1;
			break;
		case 0x2b:
			regDst.i  =-1;
			regWrite.i=0;
			ALUSrc.i  =1;
			ALUOp.i   =0x20;
			memWrite.i=1;
			memRead.i =0;
			memToReg.i=-1;
			break;
		//////////////////////
		case 0x2:
		//////////////////////
		case 0x4:
		case 0x5:
			regDst.i  =-1;
			regWrite.i=0;
			ALUSrc.i  =0;
			ALUOp.i   =0x22;
			memWrite.i=0;
			memRead.i =0;
			memToReg.i=-1;
			break;
		case 0x8:
		case 0x9:
			funcCode.i       =0x20;
			funcCodeAvailable=true;
			break;
		case 0xa:
		case 0xb:
			funcCode.i       =0x22;
			funcCodeAvailable=true;
			break;
		case 0xc:
			funcCode.i       =0x24;
			funcCodeAvailable=true;
			break;
		case 0xd:
			funcCode.i       =0x25;
			funcCodeAvailable=true;
			break;
		case 0xe:
			funcCode.i       =0x26;
			funcCodeAvailable=true;
			break;
		case 0x14:
			funcCode.i       =0x4;
			funcCodeAvailable=true;
			break;
		case 0x16:
			funcCode.i       =0x6;
			funcCodeAvailable=true;
			break;
		case 0x17:
			funcCode.i       =0x7;
			funcCodeAvailable=true;
			break;
		case 0x18:
			funcCode.i       =0x28;
			funcCodeAvailable=true;
			break;
		case 0x19:
			funcCode.i       =0x29;
			funcCodeAvailable=true;
			break;
		case 0x1a:
			funcCode.i       =0x2a;
			funcCodeAvailable=true;
			break;
		case 0x1b:
			funcCode.i       =0x2b;
			funcCodeAvailable=true;
			break;
		case 0x1c:
			funcCode.i       =0x2c;
			funcCodeAvailable=true;
			break;
		case 0x0:
		case 0x1:
			regDst.i  =1;
			regWrite.i=1;
			ALUSrc.i  =0;
			funcCodeAvailable=true;
			memWrite.i=0;
			memRead.i =0;
			memToReg.i=0;
			break;
		}
		if(funcCodeAvailable)
			ALUOp.i=funcCode.i;
	}
	void dump(){
		System.out.println("--------");
		System.out.println("ControlUnit:");
		System.out.println("opCode:"+opCode);
		System.out.println("funcCode:"+funcCode);
		System.out.println("regDst:"+regDst);
		System.out.println("regWrite:"+regWrite);
		System.out.println("ALUSrc:"+ALUSrc);
		System.out.println("ALUOp:"+ALUOp);
		System.out.println("memWrite:"+memWrite);
		System.out.println("memRead:"+memRead);
		System.out.println("memToReg:"+memToReg);
		System.out.println("--------");
	}
}